//** 当前文件用于抓取详情页  **//
/* ------------------------- 判断是否登录 ------------------------- */
isLogin=false //标志
/* ------------------------- 判断是否登录 ------------------------- */
let initSetting={
    isLogin:function (e){
        if(document.querySelector(".zx_top_login2").style.display!=='none'){
            isLogin=true
        }
    },
    attribute:["|?|"]
}
let initGetScript={
  "名称":function (e){
      return isLogin ? document.querySelector(".detail_title").innerText :
      document.querySelector(".item-tit.jq_lijichakan").innerText
  },
  "时间":function (e){
     return isLogin ? document.querySelector(".detail_function #date").innerText:
          document.querySelector(".shareL.fl span").innerText
  },
  "地点":function (e){
     return isLogin?document.querySelector(".detail_function #sheng").innerText :
         document.querySelector(".shareL.fl a span").innerText
  },
  "类别":function (e){
     return  isLogin?"":document.querySelectorAll(".main-left-title *")[1].innerText
  },
  "浏览量":function (e){
     return  document.querySelector(".detail-intro-meta").children[2].innerText
  },
  "文章内容":function (e){
     return  document.querySelector(".detail-article").innerText
  },
  "基本信息":function (e){
    let infoTable = document.querySelector(".xinxi_collect-table")
    let infoTempObj=[]
    let info={}
    Array.from(document.querySelectorAll(".xinxi_collect-table td")).map(v=>{
        if(!info.title){
          info.title=v.innerText.trim()
         }else{
           info.value=v.innerText.trim()
           infoTempObj.push(info)
           info={}
        }
    })
    return infoTempObj
  },
  "公司相关信息":function (e){
    let companyObj=[]
    let companyDetail={}

    //公司名称
    companyDetail.title = "公司名称"
    companyEle = document.querySelector(".qiyeshangqing_cont div")
    if(companyEle){
        companyDetail.value = companyEle.innerHTML
        companyObj.push(companyDetail)
    }

    Array.from(document.querySelectorAll(".qiyeshangqing_cont div .result-num")).map(v=>{
        let companyDetail={}
        let arr = v.innerText.split(" ")
        companyDetail.title=arr[0]
        companyDetail.value=arr[1]
        companyObj.push(companyDetail)
    })
    console.log(companyObj,companyDetail)
    return companyObj
  },
  "项目相关信息":function (e){
    let projectObj=[]
    let detail={}

    //公司名称
    detail.title = "公司名称"
    detail.value = document.querySelector(".qiyeshangqing_cont div").innerHTML
    projectObj.push(detail)


    Array.from(document.querySelectorAll(".qiyeshangqing_cont div .result-num")).map(v=>{
    	let detail={}
    	let arr = v.innerText.split(" ")
        detail.title=arr[0]
        detail.value=arr[1]
        projectObj.push(detail)
    })
    return projectObj
  },
  "招标相关信息":function (e){
    let callBidsObj=[]
    let callBidsDetail={}
    if(!isLogin) /* 如果没有录的话 */ {
    	let str = ""
    	Array.from(document.querySelector(".zdynr").getElementsByTagName("*")).map(v=>{
    		if(v.localName=="p" || v.localName=="table"){
    			if(v.innerText.replace(/[\s\n\t]+/g, '').length != 0){
    			  str+=v.innerText.replace(/[\s\n\t]+/g, '')+"\n"
    			}
    		}
    	})
    	callBidsObj.push({title:"招标正文",value:document.querySelector(".zdynr").innerText})
    }
    return callBidsObj
  }
}

/**
 * 异常捕获的公共方法
 * @param tryEve
 * @param catchEve
 */
function tryCatch(tryEve,catchEve){
    try {
      tryEve()
    }catch (e){
      catchEve()
    }
}

//申明数组
let pageInfo=[]
let keys = Object.keys(initGetScript)
Array.from(keys).map((k)=>{
    tryCatch(()=>{
        let callBack = initGetScript[k]()
        if(Array.isArray(callBack)){
            pageInfo=[...pageInfo,...callBack]
        }else{
           pageInfo.push({title:k,value:callBack})
        }
    },()=>{
        pageInfo.push({title:k,value:""})
    })

})

// ---------------------- 参数校验 -------------------------- //
Array.from(pageInfo).map(v=>{
	if(!v["value"]){v["value"]=""}
})
console.log("数据补齐之前:",pageInfo)
let jsonStr = JSON.stringify(pageInfo)
Array.from(initSetting.attribute).map(v=>{
	if(jsonStr.indexOf(`"title":"${v}"`) ===-1){
		pageInfo.push({title:v,value:""})
	}
})

let infoObj = pageInfo
console.log("数据补齐之后:",infoObj)